package pl.myszon.RILLib;

import java.util.Map;

/**
 *
 * @author Myszon
 */
public class ReadItLater {

    private Config config = new Config();;
    private static ReadItLater singleton = null;

    /**
     *
     * @return
     * @throws APIKeyNeededException
     */
    public static ReadItLater getInstance() throws APIKeyNeededException {
        if(singleton == null)
            throw new APIKeyNeededException();

        return singleton;
    }

    /**
     * 
     * @param APIKey your API key
     * @return
     */
    public static ReadItLater getInstance(String APIKey) {
        if(singleton == null) {
            singleton = new ReadItLater(APIKey);
        }

        return singleton;
    }

    private ReadItLater(String APIKey) {
        config.setAPIKey(APIKey);
    }

    public boolean setUser(String username, String password) throws ConnectionFailureException {
        config.setUser(username, password);
        Connection conn = new Connection(config);
        return conn.isAuthenticationSuccessful();
    }

    public ServerAnswer<Map> getStatus() throws ConnectionFailureException {
        Connection conn = new Connection(config);
        Response response = conn.getStatus();
        ResponseParser parser = new StatusParser();
        return parser.parse(response);
    }

    /**
     * Adding new website to RIL.
     * Throws exception if you use incorrect URL (checks syntax, if you url can be parsed by URL class it will work) or forget to set title.
     * @param website website you want to add
     * @return ServerAnswer<String> object. Status field contains status code, content - website.url
     * @throws ConnectionFailureException
     */
    public ServerAnswer<String> addLink(Website website) throws ConnectionFailureException {
        Connection conn = new Connection(config);
        Response response = conn.addLink(website);
        ResponseParser parser = new WebsiteAddedParser();
        ServerAnswer result = parser.parse(response);

        return new ServerAnswer<String>(result.status,website.url);
    }
}
